<template>
	<div class="kaochangguanli flex">
    <left></left>
    <div style="width: calc(100vw - 280px); height: calc(100vh - 40px); overflow: auto; padding: 20px;">
      <div class="mb20"><i class="el-icon-arrow-left" style="margin-right: 10px; font-weight: bold; cursor: pointer;" @click="$router.back(-1)"></i>{{$route.query.name}}报名名单</div>
      <div class="flexc" style="margin-bottom: 20px;">
        <el-input v-model="search.name" style="width: 250px; " placeholder="请输入姓名"></el-input>
        <el-select v-model="search.type" multiple style="width: 250px; margin-left: 20px;" placeholder="请选择组别">
            <el-option label="初中生组" value="1"></el-option>
            <el-option label="高中生组" value="2"></el-option>
            <el-option label="高职生组" value="3"></el-option>
            <el-option label="大学生组" value="4"></el-option>
            <el-option label="成人组" value="5"></el-option>
            <el-option label="小学组" value="6"></el-option>
            <el-option label="学生组" value="7"></el-option>
        </el-select>
        <el-button type="primary" round style="margin-left: 20px;" @click="sousuo">搜索</el-button>

        <el-button type="primary" round style="margin-left: 20px;" @click="daochu">导出报名单</el-button>

        <el-upload :show-file-list="false" action="#" ref="upload" :http-request="uploasd" style="margin-left: 20px;">
          <el-button type="primary" round icon="el-icon-plus">导入成绩</el-button>
        </el-upload>

        <el-upload :show-file-list="false" action="#" ref="upload" :http-request="uploasd2" style="margin-left: 20px;">
          <el-button type="primary" round icon="el-icon-plus">导入要报名的学生信息</el-button>
        </el-upload>

        <div style="margin-left: 20px; color: #409EFF; font-size: 12px;" @click="xiazai('https://file.storep.91haoka.cn/daorumoban.xlsx?attname=')">模板下载</div>

        <el-button type="primary" round style="margin-left: 20px;" @click="chakankai">查看导入名单</el-button>

        <!-- <div style="margin-left: 20px; color: #409EFF; font-size: 12px;" @click="xiazai('https://file.storep.91haoka.cn/gantanhao/chengjidaoru.xlsx?attname=')">模板下载</div> -->
      </div>

      <el-table :data="tableData"  border  style="width: 100%">

          <el-table-column prop="name" label="用户名称"></el-table-column>
          <el-table-column prop="unity" label="学校"></el-table-column>
          <el-table-column prop="birthday" label="出生日期"></el-table-column>
          <el-table-column prop="id_card" label="身份证号"></el-table-column>
          <el-table-column prop="email" label="email"></el-table-column>
          <el-table-column prop="mobile" label="手机型号"></el-table-column>
          <!-- <el-table-column prop="channel" label="channel"></el-table-column> -->
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <el-table-column prop="work_id" label="学号/工号"></el-table-column>
          <el-table-column prop="mentor_name" label="导师姓名"></el-table-column>
          <el-table-column prop="mentor_phone" label="导师手机号"></el-table-column>
          <el-table-column label="性别">
            <template slot-scope="scope">
              <div v-if="scope.row.sex == 1">男</div>
              <div v-if="scope.row.sex == 2">女</div>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="地址"></el-table-column>
          <el-table-column prop="country" label="国籍"></el-table-column>
          <el-table-column prop="post_address" label="邮寄地址"></el-table-column>
          <el-table-column prop="post_code" label="邮编"></el-table-column>
          <el-table-column prop="create_time" label="报名时间"></el-table-column>
          <el-table-column prop="type_name" label="组别"></el-table-column>
          <el-table-column prop="price" label="支付价格"></el-table-column>
          <el-table-column prop="pay_status_name" label="支付状态"></el-table-column>
          <el-table-column prop="inv_name" label="邀请人"></el-table-column>
          <el-table-column prop="channel_name" label="渠道"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- <el-button @click="bianji(scope.row)" type="text">修改</el-button> -->
              <el-button @click="tuikuan(scope.row)" type="text" v-if="scope.row.pay_status_name == '已支付' || scope.row.pay_status_name == '申请退款中'">退款</el-button>
            </template>
          </el-table-column>

<!--          <el-table-column prop="phone" label="参赛时间"></el-table-column>
          <el-table-column prop="phone" label="比赛用时"></el-table-column>
          <el-table-column prop="phone" label="分数"></el-table-column>
          <el-table-column prop="phone" label="排名"></el-table-column> -->
      </el-table>

      <el-pagination style="text-align: right; padding: 20px;" background layout="prev,pager,next,total,jumper" :current-page="search.page" @current-change="fanye" :page-size="search.page_size" :total="total"></el-pagination>

    </div>

    <el-dialog  title="成绩上传结果"  :visible.sync="log2"  width="600px" :close-on-click-modal="false" :close-on-press-escape="false">

      <div v-for="item in zhanshi" :key="item" style="margin-bottom: 10px;">
        {{item}}
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" round @click='log2 = false'>关闭</el-button>
      </div>

    </el-dialog>


    <el-dialog  title="查看名单"  :visible.sync="chakanlog"  width="1500px" :close-on-click-modal="false" :close-on-press-escape="false">

      <el-table :data="tableData3"  border  style="width: 100%">

          <el-table-column prop="name" label="用户名称"></el-table-column>
          <el-table-column prop="unity" label="学校"></el-table-column>
          <el-table-column prop="birthday" label="出生日期"></el-table-column>
          <el-table-column prop="id_card" label="身份证号"></el-table-column>
          <el-table-column prop="email" label="email"></el-table-column>
          <el-table-column prop="mobile" label="手机型号"></el-table-column>
          <!-- <el-table-column prop="channel" label="channel"></el-table-column> -->
          <el-table-column prop="phone" label="手机号"></el-table-column>
          <el-table-column prop="work_id" label="学号/工号"></el-table-column>
          <el-table-column prop="mentor_name" label="导师姓名"></el-table-column>
          <el-table-column prop="mentor_phone" label="导师手机号"></el-table-column>
          <el-table-column label="性别">
            <template slot-scope="scope">
              <div v-if="scope.row.sex == 1">男</div>
              <div v-if="scope.row.sex == 2">女</div>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="地址"></el-table-column>
          <el-table-column prop="country" label="国籍"></el-table-column>
          <el-table-column prop="post_address" label="邮寄地址"></el-table-column>
          <el-table-column prop="post_code" label="邮编"></el-table-column>
          <el-table-column prop="create_time" label="报名时间"></el-table-column>
          <el-table-column prop="type_name" label="组别"></el-table-column>
<!--          <el-table-column prop="price" label="支付价格"></el-table-column>
          <el-table-column prop="pay_status_name" label="支付状态"></el-table-column>
          <el-table-column prop="inv_name" label="邀请人"></el-table-column>
          <el-table-column prop="channel_name" label="渠道"></el-table-column> -->
        </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" round @click='chakanlog = false'>关闭</el-button>
      </div>

    </el-dialog>

	</div>
</template>

<script>
  import chuantu from '../../components/chuantu.vue'
  import fuwenben from '../../components/fuwenben.vue'
  import left from '../left.vue'
	export default {
		name: 'kaochangguanli',
    components:{
      left,chuantu,fuwenben
    },
		data() {
			return {
        tableData:[],
        search:{
          name:"",
          type:"",
          competition_id:this.$route.query.id,
          page:1,
          page_size:20,
        },
        total:0,

        log2:false,
        all2:{
          title:"",
          content:"",
          id:"",
          img_url:""
        },
        relo:true,
        dangqian:{

        },
        zhanshi:"",
        chakanlog:false,
        tableData3:[]

			}
		},
		created() {
      this.getlist()
		},
		mounted() {

		},
		methods: {
      chakankai:function(){
        axios.get(this.IP_URL+"/admin/plan/list", {competition_id:this.$route.query.id})
          .then((response) => {
            if (response.data.msg.code == 0) {
              this.tableData3 = response.data.data.data
              this.chakanlog = true
            } else {
              this.$message.error(response.data.msg.info);
            }
          });
      },
      tuikuan:function(item){
        axios.post(this.IP_URL+"/admin/pay/refund", {id:item.p_order_id})
          .then((response) => {
            if (response.data.msg.code == 0) {
              this.$message.success('退款成功');
              this.getlist()
            } else {
              this.$message.error(response.data.msg.info);
            }
          });
      },
      daochu:function(){
        axios.post(this.IP_URL+"/admin/enrolment/export", {competition_id:this.$route.query.id})
          .then((response) => {
            if (response.data.msg.code == 0) {
              window.location.href = response.data.data.url
            } else {
              this.$message.error(response.data.msg.info);
            }
          });
      },
      xiazai:function(url){
        window.location = url
      },
      formatDate(numb, format) {
          //参数numb是间隔天数，format是自定义的日期分隔符。
          console.log(numb)
          //打印出来的结果是44563，表示1900年1月1日到当前日期的天数
          let getDay = numb - 1
          let t = Math.round((getDay - Math.floor(getDay)) * 24 * 60 * 60)
          //当导入的excel日期中包含了时分秒，t则表示小数点后面的时间
          let time = new Date(1900, 0, getDay, 0, 0, t);
          //getDay是从1900开始计算的，因此new Date()的第一个参数是1900
          let year = time.getFullYear();
          let month = time.getMonth() + 1;
          let date = time.getDate();
          return year + format + (month < 10 ? "0" + month : month) + format + (date < 10 ? "0" + date : date);
      },
      uploasd2:function(file){

        const getExcelTime = (timeNumber) => {
          let addDate = (m) => m < 10 ? '0' + m : m;
          let getDateXlsx = getFormatDateXlsx(timeNumber);
          let YYYY = getDateXlsx.getFullYear();
          let MM = addDate(getDateXlsx.getMonth() + 1);
          let DD = addDate(getDateXlsx.getDate());
          let hh = addDate(getDateXlsx.getHours());
          let mm = addDate(getDateXlsx.getMinutes());
          let ss = addDate(getDateXlsx.getSeconds());
          return `${YYYY}-${MM}-${DD}`;
        }
        const getFormatDateXlsx = (timeNumber) => {
          let utcDays = Math.floor(timeNumber - 25569);
          let utcValue = utcDays * 86400;
          let dateInfo = new Date(utcValue * 1000);
          let fractionalDay = timeNumber - Math.floor(timeNumber) + 0.0000001;
          let totalSeconds = Math.floor(86400 * fractionalDay);
          let seconds = totalSeconds % 60;
          totalSeconds -= seconds;
          let hours = Math.floor(totalSeconds / (60 * 60));
          let minutes = Math.floor(totalSeconds / 60) % 60;
          let DateTime = new Date(dateInfo.getFullYear(), dateInfo.getMonth(), dateInfo.getDate(), hours, minutes, seconds);
          return DateTime;
        }


        var that = this
        let ff = this.duwenjian(file);
        ff.then((res) => {
          let data = [];
          res.forEach((item) => {
            let sex = 1
            if(item["性别"] == '女'){
              sex = 2
            }
            let zubie = 5
            if(item["组别"] == '学生组'){
              zubie = 7
            }
            let obj = {
              name: item["姓名"],
              sex: sex,
              type: zubie,
              birthday: getExcelTime(item["出生日期"]),
              id_card: item["身份证号"],
              email: item["邮箱"],
              unity: item["学校/单位"],
              work_id: item["学号/工号"],
              mentor_name: item["导师姓名"],
              mentor_phone: item["导师手机号"],
              phone: item["选手手机号"],
              country: item["国籍"],
              address: item["居住地址"],
              post_address: item["邮寄地址"],
              post_code: item["邮编"]
            };
            data.push(obj);
          });
          let obj = {
            "competition_id":this.$route.query.id,
            "data":data
          }

          axios.post(this.IP_URL+"/admin/plan/import", obj)
            .then((response) => {
              if (response.data.msg.code == 0) {
                if(response.data.data.fail_datas){
                  this.$message.error(response.data.data.fail_datas);
                }else{
                  this.$message.success('上传成功');
                }


              } else {
                this.$message.error(response.data.msg.info);
              }
            });
        });


      },
      uploasd: function (file) {
        var that = this
        let ff = this.duwenjian(file);

        ff.then((res) => {
          let data = [];
          res.forEach((item) => {
            let obj = {
              enrolment_id:parseInt(item["报名ID"]),
              entry_time: that.formatDate(parseInt(item["参赛时间"]),'-'),
              play_time: item["比赛用时(分钟)"],
              score: item["成绩"],
              ranking: item["排名"],
            };
            data.push(obj);
          });
          let obj = {
            "competition_id":this.$route.query.id,
            "datas":data
          }

          axios.post(this.IP_URL+"/admin/achievement/import", obj)
            .then((response) => {
              if (response.data.msg.code == 0) {

                if(response.data.data.length != ''){
                  this.zhanshi = response.data.data
                  this.log2 = true
                }else{
                  this.$message.success('上传成功');
                }
                this.getlist()
              } else {
                this.$message.error(response.data.msg.info);
              }
            });
        });
      },
      daoru:function(){

      },
      getimgid3:function(val){
        this.all2.img_url = val
      },
      getcontent:function(val){
        this.all2.content = val
      },

      add:function(){
        this.all2 = {
          title:"",
          content:"",
          id:"",
          img_url:""
        }
        this.relo = false
        this.$nextTick(()=>{
          this.relo = true
        })
        this.log2 = true
      },

      shanchu:function(item){
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.post(this.IP_URL+'/admin/down/del',{id:item.id}).then((response)=>{
          	if (response.data.msg.code == 0) {
          		this.$message.success('操作成功');
              this.getlist()
          	} else {
          		this.$message.error(response.data.msg.info);
          	}
          })
        })
      },

      bianji2:function(item){
        axios.get(this.IP_URL+'/admin/down/info?id='+item.id).then((response)=>{
        	if (response.data.msg.code == 0) {
            if(response.data.data){
              this.all2 = JSON.parse(JSON.stringify(response.data.data))
            }else{
              this.all2 = {
                title:"",
                content:"",
                id:"",
                img_url:""
              }
            }
            this.relo = false
            this.$nextTick(()=>{
              this.relo = true
            })
            this.log2 = true
        	} else {
        		this.$message.error(response.data.msg.info);
        	}
        })
      },

      fabu2:function(){
        axios.post(this.IP_URL+'/admin/achievement/save',this.all2).then((response)=>{
        	if (response.data.msg.code == 0) {
        		this.$message.success('操作成功');
            this.log2 = false
            this.getlist()
        	} else {
        		this.$message.error(response.data.msg.info);
        	}
        })
      },
      sousuo:function(){
        this.search.page = 1
        this.getlist()
      },
      getlist:function(){
        axios.get(this.IP_URL+'/admin/enrolment/list',{params:this.search}).then((response)=>{
        	if (response.data.msg.code == 0) {
        		this.tableData = response.data.data.data
            this.total = response.data.data.total
        	} else {
        		this.$message.error(response.data.msg.info);
        	}
        })
      },

      fanye:function(val){
        this.search.page = val
        this.getlist()
      },
		}
	}
</script>


<style scoped >

</style>
